/**
 * Created by dongp on 2016/2/3.
 */

function citycascade(province,pVal,city,cVal,town,tVal){

    var arr = ["请选择省份","请选择城市","请选择区县"];
    var p_index = 0;
    var c_index = 0;

    //默认显示
    $("<option value=''>"+arr[0]+"</option>").appendTo($("#"+province));
    $("<option value=''>"+arr[1]+"</option>").appendTo($("#"+city));
    $("<option value=''>"+arr[2]+"</option>").appendTo($("#"+town));

    var i;
    //加载省份，如果传入的有值则加selected
    for(i = 0; i < mp.length; i++) {
        if(mp[i] == pVal) {
            $("<option selected value='"+mp[i]+"'>"+mp[i]+"</option>").appendTo($("#"+province));
            p_index = i+1;
        }else{
            $("<option value='"+mp[i]+"'>"+mp[i]+"</option>").appendTo($("#"+province));
        }
    }

    //根据获得的省,加载市
    if(p_index != 0) {
        for(i = 0; i < mc[p_index-1].length; i++) {
            if(mc[p_index-1][i] == cVal) {
                c_index = i+1;
                $("<option selected value='"+mc[p_index-1][i]+"'>"+mc[p_index-1][i]+"</option>").appendTo($("#"+city));
            }else {
                $("<option value='"+mc[p_index-1][i]+"'>"+mc[p_index-1][i]+"</option>").appendTo($("#"+city));
            }
        }
    }

    //根据获得的市，加载区县
    if(c_index != 0) {
        for(i = 0; i < mh[p_index-1][c_index-1].length; i++) {
            if(mh[p_index-1][c_index-1][i] == tVal) {
                $("<option selected value='"+mc[p_index-1][c_index-1][i]+"'>"+mh[p_index-1][c_index-1][i]+"</option>").appendTo($("#"+town));
            }else {
                $("<option value='"+mc[p_index-1][c_index-1][i]+"'>"+mh[p_index-1][c_index-1][i]+"</option>").appendTo($("#"+town));
            }
        }
    }

    $("#"+province).change(function(){
        var i = 0;
        p_index = $("#"+province).get(0).selectedIndex;
        //清空城市
        $("#"+city).empty();
        $("<option value=''>"+arr[1]+"</option>").appendTo($("#"+city));
        if(p_index != 0) {
            for(i = 0; i < mc[p_index-1].length; i++) {
                $("<option value=''>"+mc[p_index-1][i]+"</option>").appendTo($("#"+city));
            }
        }
        //清空区县
        $("#"+town).empty();
        $("<option value=''>"+arr[2]+"</option>").appendTo($("#"+town));
    });

    $("#"+city).change(function(){
        var i = 0;
        c_index = $("#"+city).get(0).selectedIndex;
        //清空区县
        $("#"+town).empty();
        $("<option value=''>"+arr[2]+"</option>").appendTo($("#"+town));
        if(c_index != 0) {
            for(i = 0; i < mh[p_index-1][c_index-1].length; i++) {
                $("<option value='"+mh[p_index-1][c_index-1][i]+"'>"+mh[p_index-1][c_index-1][i]+"</option>").appendTo($("#"+town));
            }
        }
    });

}
